<?php
class LoginHandler {
	private $SessionLoginCheck;
	public function isLoggedIn() {
		//Returns true if user is Logged in
		if($_SESSION[$this -> SessionLoginCheck] == "loggedIn") {
			return true;
		} else {
			return false;
		}
	}

	public function doLogin($name, $password) {
		//If fields are not empty, check input against "registered" users
		if($name && $password != null) {
			switch($name){
				//User Lisa
				case "Lisa";
				if($password == "1234"){
					//If password is correct, set session variable to loggedIn and return true 
					$_SESSION[$this -> SessionLoginCheck] = "loggedIn";
					return true;
				}
				else return false;
				//User Anna
				case "Anna";
				if($password == "0000"){
					//If password is correct, set session variable to loggedIn and return true 
					$_SESSION[$this -> SessionLoginCheck] = "loggedIn";
					return true;
				}
				else return false;
				//User Karin
				case "Karin";
				if($password == "5555"){
					//If password is correct, set session variable to loggedIn and return true 
					$_SESSION[$this -> SessionLoginCheck] = "loggedIn";
					return true;
				}
				else return false;
			}
		} else {return false;}
	}
	

	public function doLogout() {
		//Set session variable to loggedOut
		// unset
		unset($_SESSION[$this -> SessionLoginCheck]);
	}

	public function test() {
		//1st test, logout
		$testMessage = "<br/>" . "<h1>Loginhandlertestet: </h1><p>";
		$this->doLogout();
		if($this->isLoggedIn() == true){
			echo " 1a testet misslyckades. "; 
			return false;
		}
		//2nd test, fail to log in
		if($this->doLogin("Anna", "wrong password") == true){
			echo" 2a testet misslyckades. "; 
			return false;
		}
		//3d test, log in successfull
		if($this->doLogin("Lisa", "1234") == false){
			echo " 3e testet misslyckades. "; 
			return false;
		}

		//4th test, check if user is logged in
		if ($this->isLoggedIn() == false){
			echo " 4e testet misslyckades. ";
			return false;
		}
		//5th test, log out
		$this->doLogout();
		if($this->isLoggedIn() == true){
			echo " 5e testet misslyckades. "; 
			return false;
		}
		//6th test, try to log in with wrong password
		if($this->doLogin("Lisa", "5555") == true){
			echo " 6e testet misslyckades. "; 
			return false;
		}
		return true;
	}
}
?>